package com.wangzeqi.neihanduanzi.home.video;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.web.WebViewActivity;
import com.wangzeqi.neihanduanzi.cache.AllLruCacheLoad;
import com.wangzeqi.neihanduanzi.entity.AllEntity;
import com.wangzeqi.neihanduanzi.entity.GroupEntity;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by dllo on 16/1/12.
 */
public class AdapterVideo extends RecyclerView.Adapter {

    private AllEntity allEntity;
    private Context context;
    private RequestQueue queue;


    public AdapterVideo(Context context) {
        this.context = context;
    }

    public void addData(AllEntity allEntity) {
        this.allEntity = allEntity;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == 1) {
            View view = LayoutInflater.from(context).inflate(R.layout.video_item, null);
            VideoViewHolder viewHolder = new VideoViewHolder(view);
            return viewHolder;
        } else if (viewType == 2) {
            View view = LayoutInflater.from(context).inflate(R.layout.video_item_two, null);
            VideoViewHolderTwo viewHolderTwo = new VideoViewHolderTwo(view);
            return viewHolderTwo;
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.null_item, null);
            VideoViewHolderNull viewHolderNull = new VideoViewHolderNull(view);
            return viewHolderNull;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        queue = Volley.newRequestQueue(context);
        AllLruCacheLoad cache = new AllLruCacheLoad();
        ImageLoader imageloader = new ImageLoader(queue, cache);
        final GroupEntity entity = allEntity.getData().getData().get(position).getGroup();
        if (getItemViewType(position) == 1 && entity != null) {
            final VideoViewHolder videoViewHolder = (VideoViewHolder) holder;
            videoViewHolder.name.setText(entity.getUser().getName());
            videoViewHolder.content.setText(entity.getContent());
            videoViewHolder.type.setText(entity.getCategory_name());
            videoViewHolder.like.setText(entity.getDigg_count());
            videoViewHolder.dislike.setText(entity.getBury_count());
            videoViewHolder.comment.setText(entity.getComment_count());
            videoViewHolder.forward.setText(entity.getShare_count());
            videoViewHolder.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            videoViewHolder.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            videoViewHolder.imageView.setImageUrl(entity.getUser().getAvatar_url(), imageloader);
            videoViewHolder.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showShare();
                }
            });
            videoViewHolder.support.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "点赞", Toast.LENGTH_SHORT).show();
                }
            });
            videoViewHolder.disSupport.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "不喜欢", Toast.LENGTH_SHORT).show();
                }
            });
            videoViewHolder.videoImageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            videoViewHolder.videoImageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            videoViewHolder.videoImageView.setImageUrl("http://p4.pstatp.com/large/" + entity.getCover_image_uri(), imageloader);
            videoViewHolder.videoImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    videoViewHolder.imageViewPlay.setVisibility(View.INVISIBLE);
                    videoViewHolder.progressBar.setVisibility(View.VISIBLE);
                    videoViewHolder.videoView.setMediaController(new MediaController(context));
                    videoViewHolder.videoView.setVideoURI(Uri.parse(allEntity.getData().getData().get(position).getGroup().getMp4_url()));
                    videoViewHolder.videoView.requestFocus();
                    videoViewHolder.videoView.start();
                    videoViewHolder.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            videoViewHolder.progressBar.setVisibility(View.INVISIBLE);
                            videoViewHolder.videoImageView.setVisibility(View.INVISIBLE);
                            videoViewHolder.videoView.setVisibility(View.VISIBLE);
                        }
                    });
                    videoViewHolder.videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                        @Override
                        public boolean onError(MediaPlayer mp, int what, int extra) {
                            Toast.makeText(context, "网路不给力啊！！！", Toast.LENGTH_SHORT).show();
                            videoViewHolder.progressBar.setVisibility(View.INVISIBLE);
                            videoViewHolder.imageViewPlay.setVisibility(View.VISIBLE);
                            return false;
                        }
                    });

                }
            });
            videoViewHolder.personHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "个人主页", Toast.LENGTH_SHORT).show();
                }
            });
            videoViewHolder.detailHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url", "http://neihanshequ.com/p" + entity.getShare_url().substring(37));
                    context.startActivity(intent);
                }
            });
            videoViewHolder.imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addDialog();
                }
            });
        } else if (getItemViewType(position) == 2 && entity != null) {
            final VideoViewHolderTwo videoViewHolderTwo = (VideoViewHolderTwo) holder;
            GroupEntity entity1 = allEntity.getData().getData().get(position).getGroup();
            videoViewHolderTwo.name.setText(entity1.getUser().getName());
            videoViewHolderTwo.content.setText(entity1.getContent());
            videoViewHolderTwo.type.setText(entity1.getCategory_name());
            videoViewHolderTwo.like.setText("" + entity1.getDigg_count());
            videoViewHolderTwo.dislike.setText("" + entity1.getBury_count());
            videoViewHolderTwo.comment.setText("" + entity1.getComment_count());
            videoViewHolderTwo.forward.setText("" + entity1.getShare_count());
            videoViewHolderTwo.imageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            videoViewHolderTwo.imageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            videoViewHolderTwo.imageView.setImageUrl(entity1.getUser().getAvatar_url(), imageloader);
            videoViewHolderTwo.commentImageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            videoViewHolderTwo.commentImageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            videoViewHolderTwo.commentImageView.setImageUrl(allEntity.getData().getData().get(position).getComments().get(0).getAvatar_url(), imageloader);
            videoViewHolderTwo.commentName.setText(allEntity.getData().getData().get(position).getComments().get(0).getUser_name());
            videoViewHolderTwo.commentLike.setText("" + allEntity.getData().getData().get(position).getComments().get(0).getDigg_count());
            videoViewHolderTwo.commentContent.setText("" + allEntity.getData().getData().get(position).getComments().get(0).getText());

            videoViewHolderTwo.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showShare();
                }
            });
            videoViewHolderTwo.support.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "点赞", Toast.LENGTH_SHORT).show();
                }
            });
            videoViewHolderTwo.disSupport.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "不喜欢", Toast.LENGTH_SHORT).show();
                }
            });
            videoViewHolderTwo.videoImageView.setDefaultImageResId(R.mipmap.ugc_tip_loading_essay);
            videoViewHolderTwo.videoImageView.setErrorImageResId(R.mipmap.ugc_tip_no_connection);
            videoViewHolderTwo.videoImageView.setImageUrl("http://p4.pstatp.com/large/" + entity1.getCover_image_uri(), imageloader);
            videoViewHolderTwo.videoImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    videoViewHolderTwo.imageViewPlay.setVisibility(View.INVISIBLE);
                    videoViewHolderTwo.progressBar.setVisibility(View.VISIBLE);
                    videoViewHolderTwo.videoView.setMediaController(new MediaController(context));
                    videoViewHolderTwo.videoView.setVideoURI(Uri.parse(entity.getMp4_url()));
                    videoViewHolderTwo.videoView.requestFocus();
                    videoViewHolderTwo.videoView.start();
                    /**
                     * 视频或者音频到结尾时触发的方法
                     */
                    videoViewHolderTwo.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            videoViewHolderTwo.progressBar.setVisibility(View.INVISIBLE);
                            videoViewHolderTwo.videoImageView.setVisibility(View.INVISIBLE);
                            videoViewHolderTwo.videoView.setVisibility(View.VISIBLE);
                        }
                    });

                    videoViewHolderTwo.videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

                        @Override
                        public boolean onError(MediaPlayer mp, int what, int extra) {
                            Toast.makeText(context, "网路不给力啊！！！", Toast.LENGTH_SHORT).show();
                            videoViewHolderTwo.progressBar.setVisibility(View.INVISIBLE);
                            videoViewHolderTwo.imageViewPlay.setVisibility(View.VISIBLE);
                            return false;
                        }
                    });

                }
            });

            videoViewHolderTwo.personHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "详情页", Toast.LENGTH_SHORT).show();

                }
            });
            videoViewHolderTwo.detailHome.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url", "http://neihanshequ.com/p" + entity.getShare_url().substring(37));
                    context.startActivity(intent);
                }
            });
            videoViewHolderTwo.imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addDialog();
                }
            });
        } else {
            VideoViewHolderNull viewHolderNull = (VideoViewHolderNull) holder;
            viewHolderNull.textView.setText(null);
        }

    }

    private void showShare() {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(context.getString(R.string.ssdk_oks_share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(context.getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(context);
    }

    @Override
    public int getItemCount() {
        if (allEntity != null) {
            if (allEntity.getData() != null) {
                return allEntity.getData().getData().size();
            }
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (allEntity.getData().getData().get(position).getGroup() != null &&
                (allEntity.getData().getData().get(position).getComments() == null ||
                        allEntity.getData().getData().get(position).getComments().size() == 0)) {
            return 1;
        } else if (allEntity.getData().getData().get(position).getGroup() != null
                && allEntity.getData().getData().get(position).getComments().size() > 0) {
            return 2;
        } else {
            return 3;
        }
    }

    class VideoViewHolder extends RecyclerView.ViewHolder {
        TextView name, like, dislike, comment, forward, content, type;
        NetworkImageView imageView, videoImageView;
        VideoView videoView;
        LinearLayout personHome;
        RelativeLayout detailHome;
        ImageButton imageButton;
        RelativeLayout share, support, disSupport;
        ImageView imageViewPlay;
        /**
         * 加载预览进度条
         */
         ProgressBar progressBar;
        public VideoViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.video_person_name);
            like = (TextView) itemView.findViewById(R.id.video_like_num);
            dislike = (TextView) itemView.findViewById(R.id.video_dislike_num);
            comment = (TextView) itemView.findViewById(R.id.video_comments_num);
            forward = (TextView) itemView.findViewById(R.id.video_forwarding_num);
            content = (TextView) itemView.findViewById(R.id.video_content);
            type = (TextView) itemView.findViewById(R.id.video_content_type);
            imageView = (NetworkImageView) itemView.findViewById(R.id.video_person_picture);
            videoView = (VideoView) itemView.findViewById(R.id.video_jokes_content);
            videoImageView = (NetworkImageView) itemView.findViewById(R.id.video_image_picture);
            personHome = (LinearLayout) itemView.findViewById(R.id.video_person_home);
            detailHome = (RelativeLayout) itemView.findViewById(R.id.video_details);
            imageButton = (ImageButton) itemView.findViewById(R.id.video_dislike_del);
            share = (RelativeLayout) itemView.findViewById(R.id.video_fx);
            support = (RelativeLayout) itemView.findViewById(R.id.video_support);
            disSupport = (RelativeLayout) itemView.findViewById(R.id.video_dis_support);
            //初始化进度条
            progressBar= (ProgressBar) itemView.findViewById(R.id.progressBar);
            progressBar.bringToFront();
            imageViewPlay = (ImageView) itemView.findViewById(R.id.play_video);
            imageViewPlay.bringToFront();

        }
    }

    class VideoViewHolderTwo extends RecyclerView.ViewHolder {
        TextView name, like, dislike, comment, forward, content, type, commentName, commentContent, commentLike;
        NetworkImageView imageView, commentImageView, videoImageView;
        VideoView videoView;
        LinearLayout personHome;
        RelativeLayout detailHome;
        ImageButton imageButton;
        RelativeLayout share, support, disSupport;
        ImageView imageViewPlay;
        /**
         * 加载预览进度条
         */
        ProgressBar progressBar;
        public VideoViewHolderTwo(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.video_person_name);
            like = (TextView) itemView.findViewById(R.id.video_like_num);
            dislike = (TextView) itemView.findViewById(R.id.video_dislike_num);
            comment = (TextView) itemView.findViewById(R.id.video_comments_num);
            forward = (TextView) itemView.findViewById(R.id.video_forwarding_num);
            content = (TextView) itemView.findViewById(R.id.video_content);
            type = (TextView) itemView.findViewById(R.id.video_content_type);
            imageView = (NetworkImageView) itemView.findViewById(R.id.video_person_picture);
            commentName = (TextView) itemView.findViewById(R.id.video_comments_person_name);
            commentImageView = (NetworkImageView) itemView.findViewById(R.id.video_comments_person_picture);
            commentContent = (TextView) itemView.findViewById(R.id.video_comments_person_text);
            commentLike = (TextView) itemView.findViewById(R.id.video_comments_like_num);
            videoView = (VideoView) itemView.findViewById(R.id.video_jokes_content);
            videoImageView = (NetworkImageView) itemView.findViewById(R.id.video_image_picture);
            personHome = (LinearLayout) itemView.findViewById(R.id.video_person_home);
            detailHome = (RelativeLayout) itemView.findViewById(R.id.video_details);
            imageButton = (ImageButton) itemView.findViewById(R.id.video_dislike_del);
            share = (RelativeLayout) itemView.findViewById(R.id.video_fx);
            support = (RelativeLayout) itemView.findViewById(R.id.video_support);
            disSupport = (RelativeLayout) itemView.findViewById(R.id.video_dis_support);
            //初始化进度条
            progressBar= (ProgressBar) itemView.findViewById(R.id.progressBar);
            progressBar.bringToFront();
            imageViewPlay = (ImageView) itemView.findViewById(R.id.play_video);
            imageViewPlay.bringToFront();

        }
    }

    class VideoViewHolderNull extends RecyclerView.ViewHolder {

        TextView textView;

        public VideoViewHolderNull(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.null_tv);
        }
    }

    private void addDialog() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_one, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(view);
        builder.setTitle("选择不喜欢的理由");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context, "我们将减少此类消息的推送", Toast.LENGTH_SHORT).show();

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }
}
